package com.bmth.alise.utils.mq;

import java.util.List;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

import com.bmth.alise.utils.commom.CommonUtil;
import com.bmth.alise.utils.conf.Configuration;

public class MQSender {

	public static void send(String queueName, List<String> messages) {
		try {
			ActiveMQConnectionFactory activeMQConnectionFactory;

			String MQUrl = Configuration.getProperty("MQUrl");
			activeMQConnectionFactory = new ActiveMQConnectionFactory(MQUrl);

			Connection connection = activeMQConnectionFactory.createConnection();
			connection.start();
			Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
			Destination sendQueue = session.createQueue(queueName);
			MessageProducer producer = session.createProducer(sendQueue);

			for (String message : messages) {
				System.out.println(">> "+message);
				producer.send(session.createTextMessage(CommonUtil.encode(message)));
			}
			session.commit();
			session.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
